from tkinter import *
from dataclasses import dataclass
import random
@dataclass
class Environment:
    DURATION = 0.002
    BALL_X = 0
    BALL_Y = 100
    BALL_RADIUS = 10
    BALL_VELOCITY_X = 2
    BALL_VELOCITY_Y = 2


@dataclass
class Border:
    left: int
    right: int
    top: int
    bottom: int
#ball property
@dataclass
class Ball:
    id:int
    x:int 
    y:int 
    velocity_x:int
    velocity_y:int
    radius:int 
    color:str
    
class GameBase:
    def __init__(self):
        self.tk = Tk()
        self.canvas =Canvas(self.tk,width=800,height=600,bd=0)
        self.canvas.pack()
        self.canvas.update()
    
    #generate ball
    def make_ball(self,x,y,radius,velocity_x,velocity_y,color='black'):
        id = self.canvas.create_rectangle(x,y,x+radius,y+radius,fill=color,outline=color)
        return Ball(id,x,y,velocity_x,velocity_y,radius,color=color)
    
    def get_rand_int(self):
        x = random.randint(5,10)
        y = random.randint(7,8)
        return (x,y)
    #generate ball
    #auto random ball velocity
    def make_ball(self,x,y,radius,color='black'):
        vxy= self.get_rand_int()
        velocity_x=vxy[0]
        velocity_y=vxy[1]
        id = self.canvas.create_rectangle(x,y,x+radius,y+radius,fill=color,outline=color)
        return Ball(id,x,y,velocity_x,velocity_y,radius,color=color)
    
    def make_wall(self,ox,oy,width,height):
        self.canvas.create_rectangle(ox,oy,ox+width,oy+height)
    
    def redraw_ball(self,ball):
        self.canvas.coords(ball.id,ball.x,ball.y,ball.x+ball.radius,ball.y+ball.radius)
    
    #move ball
    def move_ball(self,ball):
        ball.x +=ball.velocity_x 
        ball.y +=ball.velocity_y
    
    #check if ball hit wall 
    def check_hit_wall(self, ball, border):
        if(ball.x+ball.velocity_x<=border.left):
            ball.velocity_x = abs( ball.velocity_x)
                
        if(ball.x+ball.velocity_x+ball.radius>=border.right):
            ball.velocity_x = - abs( ball.velocity_x)
                
        if(ball.y+ball.velocity_y<=border.top):
            ball.velocity_y = abs( ball.velocity_y)

        if(ball.y+ball.velocity_y+ball.radius >=border.bottom):  
            ball.velocity_y = - abs( ball.velocity_y)